﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace G.Service.APITest
{
    /// <summary>
    /// APITestService 的摘要说明
    /// </summary>
    public class APITestService : BaseHandler
    {
        protected override void ProcessRequest(HttpContext context, out BLL.IResult result, string action)
        {
            switch (action)
            {
                case "getapirequestdlladdress":
                    if (!System.IO.Directory.Exists(context.Server.MapPath("API_DLL")))
                    {
                        result = new BLL.Result<string[]>() { IsOK = true, ResponseData = new string[] { } };
                    }
                    else
                    {
                        var d = context.Server.MapPath("API_DLL");
                        string[] strs = System.IO.Directory.GetFiles(d).Select(c => string.Format("APITest/API_DLL/{0}", System.IO.Path.GetFileName(c))).ToArray();
                        result = new BLL.Result<string[]>() { IsOK = true, ResponseData = strs };
                    }
                    break;
                case "getapppluginaddress":
                    if (!System.IO.Directory.Exists(context.Server.MapPath("APPPlugin_DLL")))
                    {
                        result = new BLL.Result<string[]>() { IsOK = true, ResponseData = new string[] { } };
                    }
                    else
                    {
                        var d = context.Server.MapPath("APPPlugin_DLL");
                        string[] strs = System.IO.Directory.GetFiles(d).Select(c => string.Format("APITest/APPPlugin_DLL/{0}", System.IO.Path.GetFileName(c))).ToArray();
                        result = new BLL.Result<string[]>() { IsOK = true, ResponseData = strs };
                    }
                    break;
                case "uploaddll":
                    result = CreateFile(context);
                    break;
                //case "deletedll":
                //    string fileName = QueryString["filename"];
                //    if (fileName == null)
                //        result = new BLL.Result() { Msg = "缺少filename参数！" };
                //    else
                //    {
                //        string path = context.Server.MapPath("APPPlugin_DLL");
                //    }
                //    break;
                default:
                    result = new BLL.Result() { Msg = "指定的action无效！" };
                    break;
            }
        }

        private BLL.IResult CreateFile(HttpContext context)
        {
            BLL.IResult result;
            if (context.Request.HttpMethod != "POST")
                result = new BLL.Result() { Msg = "请用Post方法！" };
            else if (context.Request.Files.Count == 0)
            {
                result = new BLL.Result() { Msg = "请添加需要上传的文件！" };
            }
            else if (context.Request.Files.Count == 1)
            {
                try
                {
                    HttpPostedFile file = context.Request.Files[0];
                    if (file.ContentType == "file/dll")
                    {
                        if (System.IO.Path.GetExtension(file.FileName) == ".dll")
                        {
                            string type = QueryString["type"];
                            if (type == null)
                            {
                                result = new BLL.Result() { Msg = "缺少type参数，目前仅支持：apirequest,appplugin" };
                            }
                            else
                            {
                                string path = string.Empty;
                                switch (type)
                                {
                                    case "apirequest":
                                        path = context.Server.MapPath("API_DLL") + "\\" + file.FileName;
                                        if (!System.IO.Directory.Exists(context.Server.MapPath("API_DLL")))
                                        {
                                            System.IO.Directory.CreateDirectory(context.Server.MapPath("API_DLL"));
                                        }
                                        break;
                                    case "appplugin":
                                        path = context.Server.MapPath("APPPlugin_DLL") + "\\" + file.FileName;
                                        if (!System.IO.Directory.Exists(context.Server.MapPath("APPPlugin_DLL")))
                                        {
                                            System.IO.Directory.CreateDirectory(context.Server.MapPath("APPPlugin_DLL"));
                                        }
                                        break;
                                    default:
                                        break;
                                }
                                if (path == string.Empty)
                                {
                                    result = new BLL.Result() { Msg = "请指定上传dll的类型，目前仅支持：apirequest,appplugin" };
                                }
                                else
                                {
                                    if (!System.IO.File.Exists(path))
                                    {
                                        file.SaveAs(path);
                                        result = new BLL.Result() { IsOK = true };
                                    }
                                    else
                                    {
                                        if (QueryString["isCoverExist"] == "1")
                                        {
                                            file.SaveAs(path);
                                            result = new BLL.Result() { IsOK = true, Msg = "已成功覆盖原文件" };
                                        }
                                        else
                                            result = new BLL.Result() { Msg = file.FileName + " 该文件已存在" };
                                    }
                                }
                            }
                        }
                        else
                        {
                            result = new BLL.Result() { Msg = "该文件不是制定类型的文件" };
                        }
                    }
                    else
                    {
                        result = new BLL.Result() { Msg = "不支持" + file.ContentType + "的ContentType" };
                    }

                }
                catch (Exception ex)
                {
                    result = new BLL.Result() { Msg = ex.Message };
                }
            }
            else
            {
                result = new BLL.Result() { Msg = "一次只能上传一个文件！" };
            }
            return result;
        }
    }
}